var app = getApp(), $ = require("../../utils/util"), api = require("../../utils/api"),
  notice = require("../../utils/notice"), loaded = false

Page({
  data: {
    show: !0,
    mingpian: !1,
    huodongbm: !1,
    yanse1: !0,
    yanse2: !0,
    yanse3: !1,
    yanse4: !1,
    yanse5: !1,
    yanse6: !1,
    idx: 0,
    mobile: "",
    title: "",
    value: '',
    fb_list: [
      {wenzi: "", hasdata: !1, img: ''}
    ],
    xlist: [],
    f_mobile: ''
  },
  onLoad: function (e) {
    console.log("===========")
    console.log(e)
    var t = this, td = t.data;
    t.setData({
      f_name: e.f_name || "",
      f_tag_id: e.f_tag_id || 0
    })
    $.isAuth() ? t.getList() : (notice.addNotification("getList", t.getList, t))
    t.gethaoma()
  },
  getList: function (e) {
    var t = this, td = t.data;
  },
  gethaoma: function () {
    var t = this
    // $.get(api.userInfo, {user_id: $.uid()}, function (e) {
    //   var u = e.data;
    //   t.setData({
    //     f_mobile: u.f_mobile
    //   })
    // })
  },
  fabu: function (e) {
    var t = this, td = t.data, firstImg, tags, title = td.title, fb_list = td.fb_list, uid = $.uid(),
      xlist = td.xlist, tags = "", content, fb_list = td.fb_list, clist = [],
      currentTab = td.currentTab, mobile = td.mobile
    for (var x = 0; x < xlist.length; x++) {
      tags += xlist[x].f_tag_id + ","
    }
    if (title && tags.lastIndexOf(',') == tags.length - 1) {
      tags = tags.substr(0, tags.length - 1)
    }
    // if (td.f_mobile.length == 0 || td.f_mobile == '') {
    // getPhoneNumber: function (e) {
    //     console.log(e.detail.iv);
    //     console.log(e.detail.encryptedData);
    //     wx.login({
    //         success: res => {
    //             console.log(e.code);
    //             wx.request({
    //                 url: 'https://wx.kite-cloud.com/index.php/api/mobile/getUserInfo',
    //                 data: {
    //                     'encryptedData': encodeURIComponent(e.detail.encryptedData),
    //                     'iv': e.detail.iv,
    //                     'uid': $.uid(),
    //                     'code': e.code
    //                 },
    //                 method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    //                 header: {
    //                     'content-type': 'application/json'
    //                 }, // 设置请求的 header
    //                 success: function (res) {
    //                     if (res.status == 1) {//我后台设置的返回值为1是正确//存入缓存即可
    //                         wx.setStorageSync('phone', res.phone);
    //                     }
    //                 },
    //                 fail: function (err) {
    //                     console.log(err);
    //                 }
    //             })
    //         }
    //     })
    // }
    //     return
    // }
    if (!title || title.length == 0) {
      $.alert("文章标题不能为空")
      return
    }
    if (!tags || tags.length == 0) {
      $.alert("文章标签不能为空")
      return
    }
    console.log("======fb_list")
    console.log(fb_list)
    for (var x = 0; x < fb_list.length; x++) {
      if (!firstImg) firstImg = fb_list[x].img
      if (!fb_list[x].hasdata) continue;
      clist.push({wenzi: fb_list[x].wenzi, img: fb_list[x].img})
    }
    // if(!firstImg){
    //     $.alert("请至少上传一张图片")
    //     return
    // }
    if (clist.length == 0) {
      $.alert("文章内容不能为空")
      return
    }
    console.log(clist)
    var p = {
      user_id: uid,
      title: td.title,
      f_tag_id: tags,
      f_node_id: currentTab == 0 ? 42 : 44,
      content: JSON.stringify(clist),
      cover_image: firstImg
    }
    p.userJson = JSON.stringify({
      f_name_key: currentTab == 0 ? td.yanse1 ? "1" : "0" : "",
      f_mobile_key: currentTab == 0 ? td.yanse2 ? "1" : "0" : "",
      f_company_key: currentTab == 0 ? td.yanse3 ? "1" : "0" : "",
      f_position_key: currentTab == 0 ? td.yanse4 ? "1" : "0" : "",
      f_position_key: currentTab == 0 ? td.yanse5 ? "1" : "0" : "",
      f_position_key: currentTab == 0 ? td.yanse6 ? "1" : "0" : "",
      f_ext1_key: currentTab == 0 ? td.item1 : "",
      f_ext2_key: currentTab == 0 ? td.item2 : "",
      zixun_mobile: currentTab == 1 ? mobile : ""
    })
    
    if (currentTab == 1 && (!mobile || mobile.length == 0)) {
      $.alert("咨询手机号不能为空")
      return;
    }
    if (currentTab == 1 && !$.checkPhone(mobile, true)) return false;
    console.log(p)
    $.post(api.publish, p, function (e) {
      if (e.code == 0) {
        var fid = e.data
        $.alertModal("文章已成功提交，请到个人中心查看文章详情", function () {
          t.setData({
            yanse1: !1,
            yanse2: !1,
            yanse3: !1,
            yanse4: !1,
            yanse5: !1,
            yanse6: !1,
            idx: 0,
            mobile: "",
            title: "",
            value: '',
            fb_list: [
              {wenzi: "", hasdata: !1, img: ''}
            ],
            xlist: [],
          })
          wx.navigateTo({url: '/pages/preview/preview?id=' + fid})
        })
      }
    });
  },
  navbar: function (e) {
    console.log(e)
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  biaoqian: function () {
    wx.navigateTo({
      url: '../bqxuanze/bqxuanze'
    })
  },
  titleInput: function (e) {
    console.log(e)
    this.setData({title: e.detail.value})
  },
  itemInput1: function (e) {
    console.log(e)
    this.setData({item1: e.detail.value})
  },
  itemInput2: function (e) {
    console.log(e)
    this.setData({item2: e.detail.value})
  },
  wenzi: function (e) {
    console.log("+++++++++++wenzi")
    console.log(e)
    var idx = e.currentTarget.dataset.idx
    console.log(idx)
    wx.navigateTo({
      url: '../bianji/bianji?idx=' + idx
    })
  },
  upImg: function (e) {
    console.log(e)
    
    var t = this, td = t.data, idx = e.currentTarget.dataset.idx;
    $.loading(), wx.chooseImage({
      count: 1, sizeType: ["compressed"], success: function (n) {
        var r = n.tempFilePaths;
        wx.uploadFile({
          url: api.uploadfile,
          method: "post",
          filePath: r[0],
          formData: {user_id: $.uid()},
          name: "file",
          header: {"content-type": "multipart/form-data"},
          success: function (n) {
            $.hideloading();
            console.log(n.data)
            var j = $.parseJSON(n.data)
            // dataInfo.picList.push(j.data.replace("\\",""))
            console.log("======j=")
            console.log(j)
            console.log("======idx=" + idx)
            console.log(td.fb_list[idx])
            
            td.fb_list[idx].img = j.data
            td.fb_list[idx].hasdata = !0
            // td.fb_list[td.fb_list[idx].id]=td.fb_list[idx]
            t.setData({fb_list: td.fb_list})
          },
          fail: function (e) {
            $.hideloading()
          },
          complete: function (e) {
            $.hideloading()
          }
        })
      }, fail: function (e) {
        $.hideloading()
      }, complete: function (e) {
        $.hideloading()
      }
    })
  },
  jia: function (e) {
    var t = this, td = t.data, cout = td.total;
    cout++;
    var id = e.currentTarget.dataset.id, idx = e.currentTarget.dataset.idx;
    // if (idx == 19) return;
    // idx++
    var fb_list = td.fb_list, tmp = []
    for (var a = 0; a < fb_list.length; a++) {
      if (a < idx) {
        tmp.push(fb_list[a]);
      } else if (a == idx) {
        tmp.push(fb_list[a]);
        tmp.push(this.createTuwen(cout));
      } else {
        tmp.push(fb_list[a]);
      }
    }
    t.setData({
      fb_list: tmp,
      total: cout
    })
  },
  jian: function (e) {
    var t = this, td = t.data;
    var id = e.currentTarget.dataset.id, idx = e.currentTarget.dataset.idx;
    var fb_list = td.fb_list, tmp = []
    for (var a = 0; a < fb_list.length; a++) {
      if (a != idx || fb_list.length == 1) {
        tmp.push(fb_list[a]);
      }
    }
    t.setData({
      fb_list: tmp,
    })
  },
  createTuwen: function () {
    return {wenzi: "", hasdata: !1, img: ''};
  },
  tupian: function () {
    wx.chooseImage({
      count: 2, sizeType: ['original'], sourceType: ['album', 'camera'],
      success: function (e) {
        console.log(e)
      }
    })
  },
  haoma: function (e) {
    console.log(e)
    this.setData({
      mobile: e.detail.value
    })
  },
  wancheng: function () {
    var t = this, td = t.data
    var phone = td.value
    if (phone.length == 0) {
      wx.showModal({
        title: "提示",
        content: '请填写个人联系电话',
        showCancel: false
      })
    }
    if (phone.length > 0 && !$.checkPhone(phone, true)) return false;
    var ck = function (e) {
      wx.showModal({
        title: '成功',
        content: '正在提交信息',
        showCancel: false,
        success: function (e) {
          wx.navigateBack({})
        }
      })
    }
    // $.post(api.addFeedback, phone, ck)
  },
  botton1: function () {
    this.setData({yanse1: !this.data.yanse1})
  },
  botton2: function () {
    this.setData({yanse2: !this.data.yanse2})
  },
  botton3: function () {
    this.setData({yanse3: !this.data.yanse3})
  },
  botton4: function () {
    this.setData({yanse4: !this.data.yanse4})
  },
  botton5: function () {
    this.setData({yanse5: !this.data.yanse5})
  },
  botton6: function () {
    this.setData({yanse6: !this.data.yanse6})
  },
  queren: function () {
    this.setData({
      huodongbm: !this.data.huodongbm
    })
  },
  lianximp: function () {
    this.setData({
      huodongbm: !this.data.huodongbm
    })
  },
  queding: function () {
    this.setData({
      // huodongbm:!this.data.huodongbm
      mingpian: !this.data.mingpian
    })
  },
  huodongbaoming: function () {
    this.setData({
      mingpian: !this.data.mingpian
    })
  },
})
